﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Core.Extensions;
using System.Linq;
using System.Collections.Generic;
using nERP.Model.So;
using nERP.Model.Bas;
using nERP.Model.Wms;
using nERP.ViewModel.Wms.SaleReturnInWarehouseVMs;
using nERP.Model;
using nERP.Model.Po;

namespace nERP.Wms.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.SaleReturnInWarehouse")]
    [ApiController]
    [Route("/api/Wms/SaleReturnInWarehouse")]
    public partial class SaleReturnInWarehouseController : BaseApiController
    {
        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<SaleReturnInWarehouseVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(SaleReturnInWarehouseVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
                if (ModelState.IsValid)
                {
                    await vm.StartWorkflowAsync();
                }

                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(SaleReturnInWarehouseVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }

        [ActionDescription("Sys.Approve")]
        [Public]
        [HttpPost("[action]")]
        public async Task<ActionResult> Approve(SaleReturnInWarehouseVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (ModelState.IsValid)
                {
                    await vm.ContinueWorkflowAsync(vm.ActionName, vm.Remark);
                }
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }


        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(SaleReturnInWarehouseBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

        [HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<SaleReturnInWarehouseBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<SaleReturnInWarehouseImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(SaleReturnInWarehouseImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }




        [HttpGet("GetSaleReturns")]
        [AllRights]
        public ActionResult GetSaleReturns()
        {
            return Ok(DC.Set<SaleReturn>().GetSelectListItems(Wtm, x => x.SaleReturnCode, x => x.SaleReturnCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSaleReturnBySaleReturnId(List<string> id)
        {
            var rv = DC.Set<SaleReturn>().CheckIDs(id).GetSelectListItems(Wtm, x => x.SaleReturnCode, x => x.SaleReturnCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSaleReturnInWarehouseBySaleReturn(List<string> id)
        {
            var rv = DC.Set<SaleReturnInWarehouse>().CheckIDs(id, x => x.SaleReturnId).GetSelectListItems(Wtm, x => x.SaleReturnInWarehouseCode, x => x.SaleReturnInWarehouseCode);
            return Ok(rv);
        }


        [HttpGet("GetSales")]
        [AllRights]
        public ActionResult GetSales(string SaleReturnCode)
        {
            return Ok(DC.Set<SaleReturn>().Where(x => x.SaleReturnCode.Equals(SaleReturnCode)).GetSelectListItems(Wtm, x => x.SaleId, x => x.SaleId.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSaleBySaleId(List<string> id)
        {
            var rv = DC.Set<Sale>().CheckIDs(id).GetSelectListItems(Wtm, x => x.SaleCode, x => x.SaleCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSaleReturnInWarehouseBySale(List<string> id)
        {
            var rv = DC.Set<SaleReturnInWarehouse>().CheckIDs(id, x => x.SaleId).GetSelectListItems(Wtm, x => x.SaleReturnInWarehouseCode, x => x.SaleReturnInWarehouseCode);
            return Ok(rv);
        }


        [HttpGet("GetCustomers")]
        [AllRights]
        public ActionResult GetCustomers(string SaleReturnCode)
        {
            return Ok(DC.Set<Customer>().Where(x => x.CustomerCode.Equals(
                DC.Set<SaleReturn>().Where(z0 => z0.SaleReturnCode.Equals(SaleReturnCode)).Select(z0 => z0.CustomerId).SingleOrDefault())).GetSelectListItems(Wtm, x => x.CustomerName, x => x.CustomerCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetCustomerByCustomerId(List<string> id)
        {
            var rv = DC.Set<Customer>().CheckIDs(id).GetSelectListItems(Wtm, x => x.CustomerName, x => x.CustomerCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSaleReturnInWarehouseByCustomer(List<string> id)
        {
            var rv = DC.Set<SaleReturnInWarehouse>().CheckIDs(id, x => x.CustomerId).GetSelectListItems(Wtm, x => x.SaleReturnInWarehouseCode, x => x.SaleReturnInWarehouseCode);
            return Ok(rv);
        }


        [HttpGet("GetFrameworkUsers")]
        [AllRights]
        public ActionResult GetFrameworkUsers()
        {
            return Ok(DC.Set<FrameworkUser>().GetSelectListItems(Wtm, x => x.Name, x => x.ITCode.ToString()));
        }
        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetFrameworkUserByFrameworkUserId(List<string> id)
        {
            var rv = DC.Set<FrameworkUser>().CheckIDs(id).GetSelectListItems(Wtm, x => x.Name, x => x.ITCode.ToString());
            return Ok(rv);
        }

        [HttpPost("[action]")]
        [AllRights]
        public ActionResult Select_GetSaleReturnInWarehouseByFrameworkUser(List<string> id)
        {
            var rv = DC.Set<SaleReturnInWarehouse>().CheckIDs(id, x => x.MakeUserId).GetSelectListItems(Wtm, x => x.SaleReturnInWarehouseCode, x => x.SaleReturnInWarehouseCode);
            return Ok(rv);
        }


    }
}